package com.liuyk.commonlib.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.liuyk.commonlib.utility.PermissionUtils;
import com.liuyk.commonlib.utility.StatusBarUtil;
import com.liuyk.commonlib.widget.SlideLayout;

/**
 * 支持全屏沉浸式
 * <p>
 * Created by liuyk on 2020/10/16.
 */
public abstract class BaseAppCompatActivity extends AppCompatActivity {
    protected boolean isActive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.hide();
        }
        //设置屏幕方向
        if (Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        if (isSlide()) {
            SlideLayout slideLayout = new SlideLayout(this);
            slideLayout.bind();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        isActive = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        isActive = false;
        //TODO
//        LeakUtils.fixInputMethodManagerLeak(this);
        super.onDestroy();
    }

    @Override
    public void finish() {
        isActive = false;
        super.finish();
    }

    protected abstract int getLayout();

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 是否可以滑动返回
     */
    protected boolean isSlide() {
        return false;
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    //全屏沉浸式
    protected void fullscreenImmersion() {
        fullscreenImmersion(0, -1, 0, 0);
    }

    protected void fullscreenImmersion(int left, int notchHeight, int right, int bottom) {
        if (notchHeight != -1) {
            notchSetting(left, notchHeight, right, bottom);
        }
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(option);
        }
    }

    public abstract void notchSetting(int left, int notchHeight, int right, int bottom);

    //状态栏是否为亮主题(白底黑字), 同时也会全屏沉浸式，布局会顶到状态了最上层
    //如果不是亮主题，状态栏就会采用Theme里面的颜色
    public abstract boolean isStatusLightMode();

    //亮色状态栏，会变成黑色字体，背景颜色自定义
    protected void statusBarLight(int color) {
        StatusBarUtil.initStatusBar(this, true, color);
    }

    public int getNotchHeight() {
        return StatusBarUtil.getStatusBarHeight(this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionUtils.permissionCallBack(this, requestCode, permissions, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
